﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Display.MarketUI {
    import flash.events.*;
    import Engine.Managers.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Events.*;
    import flash.utils.*;
    import org.aswing.*;
    import Classes.*;
    import Classes.util.*;
    import com.greensock.*;
    import org.aswing.geom.*;
    import Display.*;
    import GameMode.*;
    import Classes.inventory.*;
    import Modules.guide.ui.*;
    import Display.aswingui.*;
    import L10n.*;

    public class Catalog extends Sprite {

        public static const TOOLTIP_WIDTH:int = 177;
        public static const CARD_WIDTH:int = 88;
        public static const CARD_HEIGHT:int = 94;
        public static const CARD_OFFSET:int = 2;
        public static const SCROLL_WIDTH:int = 750;
        public static const SCROLL_HEIGHT:int = 250;
        public static const TAB_HEIGHTOFFFSET:int = 17;
        public static const CATALOG_TWEEN_OFFSET:int = 25;
        public static const TOOLTIP_HOLDER_OFFSET_Y:int = -110;

        public static var assetDict:Dictionary;

        protected var m_type:String;
        protected var m_content:JPanel = null;
        protected var m_shown:Boolean = false;
        protected var m_itemName:String;
        protected var m_cursorId:int = 0;
        protected var jtw:JWindow;
        protected var m_tooltip:ItemCatalogToolTip;
        protected var jctw:JWindow;
        protected var m_categoryTooltip:ItemCategoryToolTip;
        protected var m_ui:ItemCatalogUI;
        protected var m_holder:Sprite;
        protected var m_exclusive:Boolean;
        protected var m_closeMarket:Boolean;
        protected var m_comObject:Object;
        protected var m_overrideTitle:String;
        protected var m_cattooltipHolder:Sprite;
        protected var m_tooltipHolder:Sprite;
        protected var m_arrow:GuideArrow;

        public function Catalog(_arg1:String, _arg2:ItemCatalogUI, _arg3:String, _arg4:Boolean, _arg5:String, _arg6:Boolean){
            this.m_ui = _arg2;
            this.m_closeMarket = _arg6;
            this.m_overrideTitle = _arg5;
            this.m_type = _arg1;
            this.m_itemName = _arg3;
            this.m_exclusive = _arg4;
            this.loadAssets();
        }
        public static function setupAssets(_arg1:DisplayObject, _arg2:String):void{
            var _local3:ItemCatalogUI = new ItemCatalogUI(true);
            var _local4:Catalog = new Catalog("", _local3, "", false, "", false);
            _local4.makeAssets(_arg1, _arg2);
        }

        protected function loadAssets():void{
            Global.delayedAssets.get(DelayedAssetLoader.MARKET_ASSETS, this.makeAssets);
        }
        protected function makeAssets(_arg1:DisplayObject, _arg2:String):void{
            this.m_comObject = _arg1;
            this.setChrome(new Event("loaded"));
        }
        protected function setChrome(_arg1:Event):void{
            Catalog.assetDict = new Dictionary(true);
            Catalog.assetDict["bg"] = this.m_comObject.marketBG;
            Catalog.assetDict["btn_left_offstate"] = this.m_comObject.market_prev_up;
            Catalog.assetDict["btn_left_onstate"] = this.m_comObject.market_prev_over;
            Catalog.assetDict["btn_left_downstate"] = this.m_comObject.market_prev_down;
            Catalog.assetDict["btn_right_offstate"] = this.m_comObject.market_next_up;
            Catalog.assetDict["btn_right_onstate"] = this.m_comObject.market_next_over;
            Catalog.assetDict["btn_right_downstate"] = this.m_comObject.market_next_down;
            Catalog.assetDict["card_available_selected"] = this.m_comObject.marketItem;
            Catalog.assetDict["card_limited_item"] = this.m_comObject.limitedMarketItem;
            Catalog.assetDict["card_limited_counter"] = this.m_comObject.limitedMarketItemCounter;
            Catalog.assetDict["card_available_unselected"] = this.m_comObject.marketItem;
            Catalog.assetDict["card_locked"] = this.m_comObject.marketItem_locked;
            Catalog.assetDict["icon_coins"] = this.m_comObject.coin;
            Catalog.assetDict["icon_cash"] = this.m_comObject.cash;
            Catalog.assetDict["icon_goods"] = this.m_comObject.goods;
            Catalog.assetDict["sale_tag"] = this.m_comObject.market_saletag;
            Catalog.assetDict["pop_lock"] = this.m_comObject.lockedArea;
            Catalog.assetDict["pop_neighborlock"] = this.m_comObject.lockedNeighborArea;
            Catalog.assetDict["tab_icon_new_onstate"] = this.m_comObject.new_big;
            Catalog.assetDict["tab_icon_new_offstate"] = this.m_comObject.new_small;
            Catalog.assetDict["tab_icon_decorRoads_onstate"] = this.m_comObject.fountain_big;
            Catalog.assetDict["tab_icon_decorRoads_offstate"] = this.m_comObject.fountain_small;
            Catalog.assetDict["tab_icon_business_onstate"] = this.m_comObject.shirt_big;
            Catalog.assetDict["tab_icon_business_offstate"] = this.m_comObject.shirt_small;
            Catalog.assetDict["tab_icon_farming_onstate"] = this.m_comObject.farming_on;
            Catalog.assetDict["tab_icon_farming_offstate"] = this.m_comObject.farming_off;
            Catalog.assetDict["tab_icon_residence_onstate"] = this.m_comObject.house_big;
            Catalog.assetDict["tab_icon_residence_offstate"] = this.m_comObject.house_small;
            Catalog.assetDict["tab_icon_expansion_onstate"] = this.m_comObject.expand_big2;
            Catalog.assetDict["tab_icon_expansion_offstate"] = this.m_comObject.expand_big;
            Catalog.assetDict["tab_icon_extras_onstate"] = this.m_comObject.energy_big;
            Catalog.assetDict["tab_icon_extras_offstate"] = this.m_comObject.energy_small;
            Catalog.assetDict["tab_icon_ships_onstate"] = this.m_comObject.ship_big;
            Catalog.assetDict["tab_icon_ships_offstate"] = this.m_comObject.ship_small;
            Catalog.assetDict["tab_icon_municipal_onstate"] = this.m_comObject.tab_icon_municipal_onstate;
            Catalog.assetDict["tab_icon_municipal_offstate"] = this.m_comObject.tab_icon_municipal_offstate;
            Catalog.assetDict["tab_icon_wonders_onstate"] = this.m_comObject.wonders_big;
            Catalog.assetDict["tab_icon_wonders_offstate"] = this.m_comObject.wonders_small;
            Catalog.assetDict["tab_icon_landmark_onstate"] = this.m_comObject.landmark_big;
            Catalog.assetDict["tab_icon_landmark_offstate"] = this.m_comObject.landmark_small;
            Catalog.assetDict["tab_icon_vehicles_onstate"] = this.m_comObject.car_big;
            Catalog.assetDict["tab_icon_vehicles_offstate"] = this.m_comObject.car_small;
            Catalog.assetDict["saleSticker"] = this.m_comObject.franchise_saleSticker;
            Catalog.assetDict["selectedTab"] = this.m_comObject.selectedTab;
            Catalog.assetDict["marketTitleRule"] = this.m_comObject.marketTitleRule;
            Catalog.assetDict["comingSoonStars"] = this.m_comObject.market_comingSoonStars;
            Catalog.assetDict["tower"] = this.m_comObject.item_tower;
            Catalog.assetDict["coliseum"] = this.m_comObject.item_coliseum;
            Catalog.assetDict["tractor"] = this.m_comObject.item_tractor;
            Catalog.assetDict["truck"] = this.m_comObject.item_truck;
            Catalog.assetDict["comingSoonBg"] = this.m_comObject.comingSoonBg;
            this.m_content = this.m_ui;
            this.asset.init(this.m_type, this, this.m_itemName, this.m_exclusive, this.m_overrideTitle);
            this.m_holder = new Sprite();
            this.addChild(this.m_holder);
            var _local2:JWindow = new JWindow(this.m_holder);
            _local2.setContentPane(this.m_ui);
            ASwingHelper.prepare(_local2);
            _local2.show();
            this.m_content.addEventListener(GenericObjectEvent.ROLLOVER_MARKET_CATEGORY, this.showCatToolTip, false, 0, true);
            this.m_content.addEventListener("hidetooltip", this.hideCatToolTip, false, 0, true);
            this.m_categoryTooltip = new ItemCategoryToolTip();
            this.m_tooltip = new ItemCatalogToolTip();
            this.m_tooltipHolder = new Sprite();
            this.addChild(this.m_tooltipHolder);
            this.m_tooltipHolder.mouseChildren = (this.m_tooltipHolder.mouseEnabled = false);
            this.m_tooltipHolder.y = -80;
            this.jtw = new JWindow(this.m_tooltipHolder);
            this.jtw.setContentPane(this.m_tooltip);
            ASwingHelper.prepare(this.jtw);
            this.m_cursorId = UI.setCursor(null);
            this.mouseEnabled = (this.mouseChildren = false);
            if (this.m_itemName != null){
                this.asset.goToItem(this.m_itemName);
            };
        }
        public function get categoryNames():Array{
            var _local3:XML;
            var _local4:String;
            var _local1:Array = new Array();
            var _local2:XMLList = Global.gameSettings.getMenuItems();
            for each (_local3 in _local2) {
                _local4 = _local3.@type;
                _local1.push(_local4);
            };
            return (_local1);
        }
        public function get type():String{
            return (this.asset.type);
        }
        public function set type(_arg1:String):void{
            this.asset.switchType(_arg1);
        }
        public function set item(_arg1:String):void{
            this.asset.goToItem(_arg1);
        }
        protected function getScrollHeight():int{
            return (Catalog.SCROLL_HEIGHT);
        }
        protected function onFullScreen(_arg1:FullScreenEvent):void{
            var _local2:Rectangle;
            var _local3:Point;
            if (parent){
                _local2 = Global.hud.getRect(parent);
                _local3 = new Point(4, ((Global.stage.stageHeight + CATALOG_TWEEN_OFFSET) - this.getScrollHeight()));
                this.x = _local3.x;
                this.y = (_local2.y + _local3.y);
            };
        }
        protected function reparentTooltipHolder(_arg1:Boolean):void{
            var _local2:Point;
            var _local3:DisplayObjectContainer;
            if (((_arg1) && (!((this.m_tooltipHolder.parent == Global.stage))))){
                _local2 = this.localToGlobal(new Point(this.m_tooltipHolder.x, this.m_tooltipHolder.y));
                _local2.y = (((Global.stage.stageHeight + CATALOG_TWEEN_OFFSET) - this.getScrollHeight()) + TOOLTIP_HOLDER_OFFSET_Y);
                _local3 = Global.stage;
            } else {
                if (((!(_arg1)) && ((this.m_tooltipHolder.parent == Global.stage)))){
                    _local2 = new Point(0, TOOLTIP_HOLDER_OFFSET_Y);
                    _local3 = this;
                };
            };
            if (_local3 != null){
                this.m_tooltipHolder.parent.removeChild(this.m_tooltipHolder);
                _local3.addChild(this.m_tooltipHolder);
                this.m_tooltipHolder.x = _local2.x;
                this.m_tooltipHolder.y = _local2.y;
            };
        }
        protected function showCatToolTip(_arg1:GenericObjectEvent):void{
            var _local2:int = ((GlobalEngine.stage.stageWidth - SCROLL_WIDTH) / 2);
            var _local3:int = ((_arg1.obj["xOffset"])!=null) ? _arg1.obj["xOffset"] : 0;
            var _local4:int = ((_arg1.obj["yOffset"])!=null) ? _arg1.obj["yOffset"] : 0;
            this.m_tooltipHolder.addChild(this.m_categoryTooltip);
            this.m_categoryTooltip.changeInfo(_arg1.obj["category"]);
            this.m_categoryTooltip.x = ((((_arg1.obj["posX"] - (this.m_categoryTooltip.width * 0.5)) - _local2) + 30) + _local3);
            this.m_categoryTooltip.y = ((120 - this.m_categoryTooltip.height) + _local4);
            Sounds.play("UI_mouseover");
        }
        protected function showToolTip(_arg1:DataItemEvent):void{
            this.reparentTooltipHolder(Global.guide.isActive());
            var _local2:Item = _arg1.item;
            var _local3:Object = _arg1.getParams();
            this.m_tooltip.changeInfo(_local2, _local3);
            ASwingHelper.prepare(this.m_tooltip);
            this.jtw.show();
            var _local4:Point = _arg1.getOffset();
            var _local5:int = ((GlobalEngine.stage.stageWidth - SCROLL_WIDTH) / 2);
            this.jtw.setX((((((_arg1.pt.x + (CARD_WIDTH / 2)) - (TOOLTIP_WIDTH / 2)) + CARD_OFFSET) - _local5) + _local4.x));
            this.jtw.setY(((195 - this.m_tooltip.getHeight()) + _local4.y));
            Sounds.play("UI_mouseover");
        }
        protected function hideCatToolTip(_arg1:Event):void{
            if (this.m_categoryTooltip.parent){
                this.m_categoryTooltip.parent.removeChild(this.m_categoryTooltip);
            };
        }
        protected function hideToolTip(_arg1:Event=null):void{
            this.jtw.hide();
        }
        protected function onShow():void{
            this.m_cursorId = UI.setCursor(null);
            if (this.m_ui != null){
                this.m_ui.addEventListener(DataItemEvent.SHOW_TOOLTIP, this.showToolTip, false, 0, true);
                this.m_ui.addEventListener("turnOffToolTip", this.hideToolTip, false, 0, true);
            };
            Global.stage.addEventListener(FullScreenEvent.FULL_SCREEN, this.onFullScreen);
            this.m_shown = true;
            Sounds.play("dialogSpawn");
            Global.ui.mouseEnabled = true;
            Global.ui.mouseChildren = true;
            UI.popupLock();
        }
        public function centerPopup():void{
            var _local1:Rectangle;
            var _local2:Point;
            if (parent){
                _local1 = Global.hud.getRect(parent);
                _local2 = new Point(4, Global.stage.stageHeight);
                this.x = _local2.x;
                this.y = (_local1.y + _local2.y);
            };
        }
        protected function getYPos():int{
            var _local1:Rectangle = Global.hud.getRect(parent);
            var _local2:int = (((_local1.y + Global.stage.stageHeight) + CATALOG_TWEEN_OFFSET) - this.getScrollHeight());
            return (_local2);
        }
        protected function showTween():void{
            var me:* = null;
            this.mouseEnabled = (this.mouseChildren = false);
            this.visible = true;
            this.centerPopup();
            var newY:* = this.getYPos();
            me = this;
            TweenLite.to(this, 0.5, {
                y:newY,
                onComplete:function ():void{
                    me.mouseChildren = (me.mouseEnabled = true);
                }
            });
        }
        protected function hideTween(_arg1:Function):void{
            var _local2:Rectangle;
            var _local3:int;
            this.mouseEnabled = (this.mouseChildren = false);
            this.asset.onTweenOut();
            if (this.m_content == null){
                if (_arg1 != null){
                    _arg1();
                };
            } else {
                _local2 = Global.hud.getRect(parent);
                _local3 = (_local2.y + Global.stage.stageHeight);
                TweenLite.to(this, 0.5, {
                    y:_local3,
                    onComplete:_arg1
                });
            };
            Sounds.play("dialogClose");
        }
        public function get asset():ICatalogUI{
            return ((this.m_content as ICatalogUI));
        }
        public function set closeMarket(_arg1:Boolean):void{
            this.m_closeMarket = _arg1;
        }
        public function set overrideTitle(_arg1:String):void{
            this.asset.overrideTitle = _arg1;
        }
        public function set exclusiveCategory(_arg1:Boolean):void{
            this.asset.exclusive = _arg1;
        }
        public function get exclusiveCategory():Boolean{
            return (this.asset.exclusive);
        }
        protected function onHide():void{
            if (this.m_shown == true){
                this.m_shown = false;
                this.mouseEnabled = (this.mouseChildren = false);
                Global.stage.removeEventListener(FullScreenEvent.FULL_SCREEN, this.onFullScreen);
                this.m_ui.removeEventListener(DataItemEvent.SHOW_TOOLTIP, this.showToolTip);
                this.m_ui.removeEventListener("turnOffToolTip", this.hideToolTip);
            };
            if (this.m_tooltip){
                this.hideToolTip();
            };
            UI.popupUnlock();
        }
        public function canBuy(_arg1:Item):Boolean{
            var _local2:Boolean;
            if (!Global.franchiseManager.isFranchiseItem(_arg1)){
                _local2 = Global.player.checkGate(_arg1.unlock, _arg1.name);
            };
            return (_local2);
        }
        public function onBuy(_arg1:Item, _arg2:Object=null):void{
            var _local6:Boolean;
            var _local7:MarketCell;
            var _local8:MarketCell;
            if (!_arg1){
                return;
            };
            Global.marketSessionTracker.trackItem();
            var _local3:Boolean;
            if (_arg1.goods > 0){
                _local3 = Global.player.canBuyCommoditiy(_arg1.GetItemSalePrice(), Commodities.GOODS_COMMODITY, false);
                if (!_local3){
                    UI.displayOutOfGoodsDialog();
                    return;
                };
            } else {
                if (_arg1.cash > 0){
                    _local3 = Global.player.canBuyCash(_arg1.GetItemSalePrice(), false);
                    if (!LocaleHelper.localSetting.sns.cash_enable){
                        _local3 = false;
                    };
                } else {
                    if (_arg1.fbc > 0){
                        _local3 = true;
                    } else {
                        _local3 = Global.player.canBuy(_arg1.GetItemSalePrice(), true);
                    };
                };
            };
            if (!_local3){
                if (!LocaleHelper.localSetting.sns.cash_enable){
                    UI.displayMessage(ZLoc.t("Dialogs", "cash_cannot_use_message"));
                    return;
                };
                if (_arg1.GetItemSalePrice() > 0){
                    UI.displayImpulseBuyPopup(ImpulseBuy.TYPE_MARKET_CASH);
                    if ((Global.world.getTopGameMode() is GMPlant)){
                        Global.world.addGameMode(new GMPlay());
                    };
                    StatsManager.count("fv_cash_buy_fail", "view_popup");
                };
                return;
            };
            var _local4:int = MarketEvent.GENERIC;
            var _local5 = "";
            switch (_arg1.type){
                case "tool":
                    _local4 = MarketEvent.EQUIPMENT;
                    break;
                case "contract":
                    _local4 = MarketEvent.CONTRACT;
                    break;
                case "residence":
                    _local4 = MarketEvent.RESIDENCE;
                    break;
                case "business":
                    _local4 = MarketEvent.BUSINESS;
                    _local6 = (_arg1.className == "LotSite");
                    if (_local6){
                        if ((_arg2 is MarketCell)){
                            _local7 = MarketCell(_arg2);
                            MarketCell.addLockCell(_local7);
                        };
                    };
                    break;
                case "starter_pack1":
                case "starter_pack2":
                case "starter_pack3":
                    _local4 = MarketEvent.FBC_PURCHASE;
                    _local5 = this.asset.type;
                    break;
                case "plot_contract":
                case "factory_contract":
                    _local4 = MarketEvent.CONTRACT;
                    if (Plot.IsMarketClickedPlotSet){
                        Plot.SetContactToMarketClickedPlot(_arg1.name);
                    };
                    break;
                case "ship_contract":
                    _local4 = MarketEvent.CONTRACT;
                    if (Ship.IsMarketClickedShipSet){
                        Ship.SetContactToMarketClickedShip(_arg1.name);
                    };
                    break;
                case "expansion":
                    _local4 = MarketEvent.EXPAND_FARM;
                    if ((_arg2 is MarketCell)){
                        _local8 = MarketCell(_arg2);
                        MarketCell.addLockCell(_local8);
                    };
                    break;
                case "energy":
                    _local4 = MarketEvent.EXTRA;
                    break;
                case "wonder":
                    _local4 = MarketEvent.WONDER;
                    break;
                case "vehicle":
                    _local4 = MarketEvent.VEHICLE;
                    break;
                case "theme_collection":
                    _local4 = MarketEvent.THEME_COLLECTION;
                    break;
                case "train":
                    _local4 = MarketEvent.TRAIN;
                    break;
                case "mystery_crate":
                    _local4 = MarketEvent.MYSTERY_CRATE;
                    break;
            };
            dispatchEvent(new MarketEvent(MarketEvent.MARKET_BUY, _local4, _arg1.name, false, false, _local5));
            if ((((this.m_closeMarket == true)) || (UI.AUTO_CLOSE_MARKET))){
                this.onCloseClick(true);
            };
        }
        public function show():void{
            this.showHelper();
        }
        protected function showHelper():void{
            this.showTween();
            this.onShow();
        }
        protected function onCloseClick(_arg1:Boolean):void{
            this.close();
            Plot.ResetMarketClickedPlotSet();
            Ship.ResetMarketClickedShipSet();
            dispatchEvent(new CloseEvent(CloseEvent.CLOSE, this.m_closeMarket));
        }
        public function close():void{
            var dispObj:* = null;
            Global.ui.swapPopupAd("genericAd", false);
            UI.showNeighbors();
            dispObj = this;
            this.hideTween(function ():void{
                if (dispObj.parent){
                    dispObj.parent.removeChild(dispObj);
                };
                dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
            });
            this.onHide();
        }
        public function updateChangedCells():void{
            this.asset.updateElements();
        }
        public function getExpandedMainMenuPosition():Vector2{
            return (new Vector2(632, 80));
        }
        public function get isShown():Boolean{
            return (this.m_shown);
        }
        public function showArrow():void{
            var _local1:Component = this.asset.shelf.dataList.getCellByIndex(1).getCellComponent();
            var _local2:IntPoint = _local1.getGlobalLocation();
            this.m_arrow = new GuideArrow(Global.getAssetURL("assets/hud/tutorialArrow.swf"), 175, 400, 100, 100, GuideArrow.ARROW_DOWN);
            this.addEventListener(MouseEvent.CLICK, this.killArrow, false, 0, true);
        }
        protected function killArrow(_arg1:MouseEvent):void{
            this.removeEventListener(MouseEvent.CLICK, this.killArrow);
            if (this.m_arrow){
                this.m_arrow.release();
            };
        }
        protected function doItemActions(_arg1:Item, _arg2:Object=null):void{
            var _local3:Boolean;
            var _local4:MarketCell;
            var _local5:MarketCell;
            switch (_arg1.type){
                case "business":
                    _local3 = (_arg1.className == "LotSite");
                    if (_local3){
                        if ((_arg2 is MarketCell)){
                            _local4 = MarketCell(_arg2);
                            MarketCell.addLockCell(_local4);
                        };
                    };
                    break;
                case "plot_contract":
                case "factory_contract":
                    if (Plot.IsMarketClickedPlotSet){
                        Plot.SetContactToMarketClickedPlot(_arg1.name);
                    };
                    break;
                case "ship_contract":
                    if (Ship.IsMarketClickedShipSet){
                        Ship.SetContactToMarketClickedShip(_arg1.name);
                    };
                    break;
                case "expansion":
                    if ((_arg2 is MarketCell)){
                        _local5 = MarketCell(_arg2);
                        MarketCell.addLockCell(_local5);
                    };
                    break;
            };
        }

    }
}//package Display.MarketUI 
